package com.leyou.trade.config;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConfigImpl;
import com.leyou.common.exceptions.LyException;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 付
 */
@Configuration
public class WxConfig {

    @Bean
    @ConfigurationProperties(prefix = "ly.pay.wx")
    public WXPayConfigImpl wxPayConfig(){
        return new WXPayConfigImpl();
    }

    @Bean
    public WXPay wxPay(WXPayConfigImpl wxPayConfig){
        try {
            return new WXPay(wxPayConfig, wxPayConfig.getNotifyUrl());
        } catch (Exception e) {
            throw new LyException(500, "初始化微信客户端失败！");
        }
    }
}
